package a09_贪心算法;

import java.util.Arrays;

/**
 * <p>
 * a12_分发糖果复习3
 * </p>
 *
 * @author flyduck
 * @since 2025/4/15
 */
public class a12_分发糖果复习3 {
    public static void main(String[] args) {
        System.out.println(candy(new int[]{1, 3, 4, 5, 2}));
    }
    public static int candy(int[] ratings) {
        int[] result = new int[ratings.length];
        Arrays.fill(result, 1);

        for (int i = 1; i < ratings.length; i++) {
            if(ratings[i] > ratings[i-1]){
                result[i] = result[i-1] + 1;
            }
        }

        for (int i = ratings.length - 2; i >= 0; i--) {
            if(ratings[i] > ratings[i+1] && result[i] <= result[i+1]){
                result[i] = result[i+1] + 1;
            }
        }

        return Arrays.stream(result).sum();
    }
}
